Convert.XML2JSON(Convert[, Pretty])

Converts an XML string or document into a JSON string.

Applies To

Description

Converts an XML string or document into a JSON string.

Properties and Methods

None

Available

The .XML2JSON(Convert[, Pretty]) method is available in:

  • 17.00.026

  • 17.01.007

  • All newer builds

Type

String

Syntax

Convert.XML2JSON(Convert[, Pretty]);

Parameters

Parameter

Required

Description

JSON

Yes

a string representing a valid JSON object

Pretty No an optional Boolean parameter to beautify the output

Example

let lcXML = `<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<Shipment>

<Header>

<ShipmentHeader>

<PstlAdr>

<PstCd>84043</PstCd>

<TwnNm>Lehi</TwnNm>

<CtrySubDvsn>UT</CtrySubDvsn>

<Ctry>US</Ctry>

<AdrLine>2912 Executive Parkway</AdrLine>

<AdrLine>Ste #325</AdrLine>

</PstlAdr>

<TradingPartnerId>DX7ALLSCOTTSLIQ</TradingPartnerId>

<ShipmentIdentification>TEST20220039300</ShipmentIdentification>

<ShipDate>2022-05-19</ShipDate>

<ShipmentTime>14:31:04</ShipmentTime>

<TsetPurposeCode>00</TsetPurposeCode>

<ShipNoticeDate>2022-05-19</ShipNoticeDate>

<ShipNoticeTime>03:44:50</ShipNoticeTime>

<ASNStructureCode>0001</ASNStructureCode>

<CarrierProNumber>123456789123</CarrierProNumber>

</ShipmentHeader>

</Header>

</Shipment>

</Document>`;

 

let loTest = Convert.XML2JSON(lcXML, true);

let lcFormName = Event.Form.Name;

 

Script.LogConsole("form name: " + lcFormName);

Script.LogConsole("\r\nXML -> JSON\r\n");

Script.LogConsole(loTest);

Script.LogConsole("\r\nJSON -> XML\r\n");

 

let lcTest = Convert.JSON2XML(loTest, true);

 

Script.LogConsole(lcTest);

 

/* Expected Output in the Web Server Console:

Console Started for Sandbox17System.

[08:38 AM] Script.LogConsole: form name: matadd

[08:38 AM] Script.LogConsole:

XML -> JSON

 

[08:38 AM] Script.LogConsole: {

"Document": {

@xmlns: "urn:iso:std:iso:20022:tech:xsd:pain.001.001.03",

@xmlns:xsi: "http://www.w3.org/2001/XMLSchema-instance",

Shipment: {

Header: {

ShipmentHeader: {

PstlAdr: {

PstCd: "84043",

TwnNm: "Lehi",

CtrySubDvsn: "UT",

Ctry: "US",

AdrLine: [

2912 Executive Parkway,

Ste #325

]

},

TradingPartnerId: "DX7ALLSCOTTSLIQ",

ShipmentIdentification: "TEST20220039300",

ShipDate: "2022-05-19",

ShipmentTime: "14:31:04",

TsetPurposeCode: "00",

ShipNoticeDate: "2022-05-19",

ShipNoticeTime: "03:44:50",

ASNStructureCode: "0001",

CarrierProNumber: "123456789123"

}

}

}

}

}

[08:38 AM] Script.LogConsole:

JSON -> XML

 

[08:38 AM] Script.LogConsole: <Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<Shipment>

<Header>

<ShipmentHeader>

<PstlAdr>

<PstCd>84043</PstCd>

<TwnNm>Lehi</TwnNm>

<CtrySubDvsn>UT</CtrySubDvsn>

<Ctry>US</Ctry>

<AdrLine>2912 Executive Parkway</AdrLine>

<AdrLine>Ste #325</AdrLine>

</PstlAdr>

<TradingPartnerId>DX7ALLSCOTTSLIQ</TradingPartnerId>

<ShipmentIdentification>TEST20220039300</ShipmentIdentification>

<ShipDate>2022-05-19</ShipDate>

<ShipmentTime>14:31:04</ShipmentTime>

<TsetPurposeCode>00</TsetPurposeCode>

<ShipNoticeDate>2022-05-19</ShipNoticeDate>

<ShipNoticeTime>03:44:50</ShipNoticeTime>

<ASNStructureCode>0001</ASNStructureCode>

<CarrierProNumber>123456789123</CarrierProNumber>

</ShipmentHeader>

</Header>

</Shipment>

</Document>`;

*/